package gui;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JWindow;

import res.StringVars;

public class SplashScreen extends JWindow implements Runnable, MouseListener{
  private static final long serialVersionUID = 1L;
  private Image capture;
  private boolean launchImmediate;
  
  public SplashScreen(boolean launchImmediate){
    super();
    this.launchImmediate = launchImmediate;
    initialize();
  }

  public void initialize(){
    setSize(516, 386);
    setAlwaysOnTop(true);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    int frmX = ((int) d.getWidth() - 516) / 2;
    int frmY = ((int) d.getHeight() - 386) / 2;
    setLocation(frmX, frmY);
    addMouseListener(this);
    
    try{
      Robot rob = new Robot();
      Rectangle rect = new Rectangle(frmX, frmY, 516, 386);
      capture = rob.createScreenCapture(rect);
    }
    catch (AWTException e){
      dispose();  
    }
    if(launchImmediate){
      setVisible(true);
      try {
        Thread.sleep(1500);
      }
      catch (InterruptedException e) {
        dispose();
      }
      dispose();
    }
  }
  
  public void paint(Graphics g){
    Graphics2D g2d = (Graphics2D)g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    Image splashImage = getToolkit().getImage(getClass().getResource("/icons/splash.png"));
    capture.getGraphics().drawImage(splashImage, 0, 0, this);
    g2d.drawImage(capture, 0, 0, this);
    g2d.setColor(new Color(60, 60, 60));
    g2d.drawString(StringVars.SPLASH_VERSION, 92, 340);
    g2d.drawString(StringVars.SPLASH_WRITTEN_BY, 400, 340);
    g2d.drawString(StringVars.SPLASH_HELP_OF, 382, 356);
    g2d.drawString(StringVars.SPLASH_DESCRIPTION, 323, 309);
  }

  public void run() {
    setVisible(true);
  }

  public void mouseClicked(MouseEvent e) {
    if(!launchImmediate){
      dispose();
    }
  }
  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}
  public void mousePressed(MouseEvent e) {}
  public void mouseReleased(MouseEvent e) {}
}
